<!DOCTYPE html>
<html>
<head>
    <title>图片上传测试</title>
    <meta charset="UTF-8">
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .upload-form {
            border: 1px solid #ddd;
            padding: 20px;
            border-radius: 5px;
            margin-bottom: 20px;
        }
        .preview {
            border: 1px solid #ddd;
            padding: 10px;
            margin-top: 10px;
            text-align: center;
        }
        .preview img {
            max-width: 200px;
            max-height: 200px;
        }
    </style>
</head>
<body>
    <h1>图片上传测试</h1>
    
    <div class="upload-form">
        <h3>测试图片上传</h3>
        <form id="uploadForm" enctype="multipart/form-data">
            <div>
                <label>选择图片文件：</label>
                <input type="file" id="testImage" name="productImage" accept="image/*">
            </div>
            <div style="margin-top: 10px;">
                <button type="button" onclick="testUpload()">测试上传</button>
            </div>
        </form>
        
        <div class="preview">
            <h4>图片预览</h4>
            <img id="previewImg" src="" style="display: none;">
            <div id="noImage">暂无图片</div>
        </div>
    </div>
    
    <div id="result"></div>

    <script>
        // 图片预览
        document.getElementById('testImage').addEventListener('change', function(e) {
            const file = e.target.files[0];
            if (file) {
                const reader = new FileReader();
                reader.onload = function(e) {
                    document.getElementById('previewImg').src = e.target.result;
                    document.getElementById('previewImg').style.display = 'block';
                    document.getElementById('noImage').style.display = 'none';
                };
                reader.readAsDataURL(file);
            }
        });

        // 测试上传
        function testUpload() {
            const fileInput = document.getElementById('testImage');
            const file = fileInput.files[0];
            
            if (!file) {
                alert('请选择图片文件');
                return;
            }
            
            const formData = new FormData();
            formData.append('productImage', file);
            formData.append('productName', '测试商品');
            formData.append('price', '99.99');
            formData.append('count', '10');
            
            fetch('/product/save', {
                method: 'POST',
                body: formData
            })
            .then(response => response.json())
            .then(data => {
                document.getElementById('result').innerHTML = 
                    '<h3>上传结果：</h3><pre>' + JSON.stringify(data, null, 2) + '</pre>';
            })
            .catch(error => {
                document.getElementById('result').innerHTML = 
                    '<h3>上传失败：</h3><pre>' + error + '</pre>';
            });
        }
    </script>
</body>
</html> 